package com.example.demo.controller;

import com.example.demo.pojo.BookNameBean;
import com.example.demo.service.impl.BookNameServiceImpl;
import com.example.demo.service.impl.BookPressServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.ResponseBody;

import javax.sound.sampled.Line;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * @Classname BookController
 * @Description TODO
 * @Date 2020/12/28 20:04
 * @Created by 新
 */
@Controller
@RequestMapping("/bookLIst")
public class BookController {
    private static final Logger logger = LoggerFactory.getLogger(BookController.class);
    @Autowired
    BookNameServiceImpl bookNameService;
    @Autowired
    BookPressServiceImpl bookPressService;

    @RequestMapping("/addBookNameList")
    @ResponseBody
    public int addBookNameList(@RequestParam(value = "list", required = false) List<String> list) {
        bookNameService.addBookNameList(list);
        return bookNameService.addBookNameList(list);
    }

    @RequestMapping("/getBookNameList")
    @ResponseBody
    public List<BookNameBean> getBookNameList(@RequestParam(value = "bookNameCon", required = false)String bookNameCon){
        return bookNameService.getBookNameList(bookNameCon);
    }
    @RequestMapping("/updateBookNameList")
    @ResponseBody
    public int updateBookNameList(@RequestParam(value = "book", required = false)String bookName,String bookNameCon){
        return bookNameService.updateBookNameList( bookName, bookNameCon);
    }
    @RequestMapping("/deleteNameBookList")
    @ResponseBody
    public HashMap deleteBookNameList(@RequestParam(value = "bookName", required = false)String bookName,@RequestParam(value = "bookPress", required = false)String bookPress){
        HashMap<String,Integer> map   =bookNameService.deleteBookNameList(bookName,bookPress);
        return map;
    }
    @RequestMapping("/deleteBookPressList")
    @ResponseBody
    public int deleteBookPressList(@RequestParam(value = "book", required = false)String book){
        return bookPressService.deleteBookPressList(book);
    }

    /**
     * 表booknamelist中取出数据写入txt文件中
     */
    public void writeToTxt() throws IOException {
        List<BookNameBean> strings=bookNameService.getBookNameList(null);
        String path="bookName.txt";
        File file = new File(path);
        //如果没有文件就创建
        if (!file.isFile()) {
            logger.info("文件创建成功");
            file.createNewFile();
        }
        BufferedWriter writer = new BufferedWriter(new FileWriter(path));
        for (BookNameBean l:strings){
            writer.write("id: "+l.getId()+"  bookName:"+l.getBookName() + "\r\n");
        }
        writer.close();
        logger.info("文件写入成功");
    }


}
